package com.jpress.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.jpress.controller.CategoriaController;
import com.jpress.exceptions.RegistroNaoEncontradoException;
import com.jpress.model.Categoria;

@FacesConverter(value="Converter.CategoriaConverter")
public class CategoriaConverter implements Converter{

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		System.out.println(value);
		try {
			Categoria categoria = (Categoria) CategoriaController.getInstance().findByNome(value);
			System.out.println(categoria.getDescricao());
			return categoria;
		} catch (RegistroNaoEncontradoException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		return null;
//		if (value == null || value.equals("")) {
//			return "";
//		} else {		
//			System.out.println("retorno " + String.valueOf(((Categoria) value).getCodigo()));
//			return String.valueOf(((Categoria) value).getCodigo());
//		}
	}
}
